<?php
namespace app\walletyhbbadmin\controller;
use Think\Db;
use think\Collection;
use Think\page;
use think\Session;
require_once (APP_PATH . 'api/FinanceApi.php');
require_once (APP_PATH . 'api/RewardApi.php');
class Member extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *会员管理
     */
    public function index()
    {
        $res = DB::name('User')->paginate(10,false,['query'=>input()]);
        $page = $res->render();
        $res=$res->all();;
        $this->assign('res',$res);
        $this->assign('page',$page);
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch();
    }
    public function doadd()
    {
        $data['u_phone'] = input('phone');
        $data['u_pwd'] = md5(input('pass'));
        $data['u_name'] = input('name');
        if( $data['u_phone'] == null)
        {
            $this->error('账号不能为空');
        }
        if( $data['u_pwd'] == null)
        {
            $this->error('密码不能为空');
        }
        if( $data['u_name'] == null)
        {
            $this->error('真实姓名不能为空');
        }
        $res = DB::name('User')->insert($data);
        $res1 = DB::name('User_us')->insert($data);
        $res2 = DB::name('User_jp')->insert($data);
        if($res && $res1 && $res2)
        {
            $this->success('添加成功','member/index');
        }
    }
    public function edit()
    {
        $id = input('id');
        $res = DB::name('User')->where('u_id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    public function doedit()
    {
        $id = input('id');
        $list = DB::name('User')->where('u_id',$id)->find();
        $data['u_phone'] = input('phone');
        $data['u_pwd'] = md5(input('pass'));
        $data['u_name'] = input('name');
        if($data['u_phone'] == null)
        {
            $this->error('账号不能为空');
        }
        if($data['u_name'] == null)
        {
            $this->error('真实姓名不能为空');
        }
        if($data['u_pwd'] == null)
        {
            $data['u_pwd'] = $list['u_pwd'];
        }
        $arr = DB::name('User')->where('u_id',$id)->update($data);
        $arr1 = DB::name('User_us')->where('u_id',$id)->update($data);
        $arr2 = DB::name('User_jp')->where('u_id',$id)->update($data);
        if($arr && $arr1 && $arr2)
        {
            $this->success('修改成功','member/index');
        }else{
            $this->error('修改密码与原密码一致','member/index');
        }
    }
    public function del()
    {
        $id = input('id');
        $res = DB::name('User')->where('u_id',$id)->delete();
        $res1 = DB::name('User_us')->where('u_id',$id)->delete();
        $res2 = DB::name('User_jp')->where('u_id',$id)->delete();
        if($res && $res1 && $res2)
        {
            $this->success('删除成功','member/index');
        }
    }

    /**
     * 基础信息
     */
    public function tuijian()
    {
        $res = DB::name('Website')->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    public function edittuijian()
    {
        $id = input('id');
        $data['w_logo_status'] = input('is_show');
        $res = DB::name('Website')->find();
        $file = request()->file('logo');
        if(!empty($file)){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' );
            $data['w_logo'] = '/public/uploads/' . str_replace('\\', '/', $info->getsaveName());
        }else{
            $data['w_logo'] == $res['w_logo'];
        }
        $files = request()->file('pic');
        if(!empty($files)){
            $info = $files->move(ROOT_PATH . 'public' . DS . 'uploads' );
            $data['w_pic'] = '/public/uploads/' . str_replace('\\', '/', $info->getsaveName());
        }else{
            $data['w_pic'] == $res['w_pic'];
        }
//        $data['w_jianjie'] = input('jianjie');
        $list = DB::name('Website')->where('w_id',$id)->update($data);
        $list1 = DB::name('Website_us')->where('w_id',$id)->update($data);
        $list2 = DB::name('Website_jp')->where('w_id',$id)->update($data);
        if($list && $list1 && $list2)
        {
            $this->success('修改成功','member/tuijian');
        }else{
            $this->error('未修改图片');
        }
    }
}